package com.sunq.studio.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * Created by sunq on 2019/6/30.
 */
public class JsonToOtherUtil {
    public static OtherPojo rtOtherPojo(String data,Object obj){

        JSONObject jsondata=JSONObject.parseObject(data);
        JSONObject jsonObject=jsondata.getJSONObject("data");
        OtherPojo otherPojo = new OtherPojo();
        otherPojo.setToken(jsonObject.getString("token"));
        otherPojo.setDeviceId(jsonObject.getString("deviceId"));

        if(obj!=null){
            String objStr = jsonObject.getString("obj");
            if(!StringUtils.isEmpty(objStr)){
                Object cls=JSONObject.parseObject(objStr, obj.getClass());
                otherPojo.setObj(cls);
            }
        }

        otherPojo.setPage(jsonObject.getIntValue("page"));
        otherPojo.setMsg(jsonObject.getString("msg"));
        return otherPojo;
    }

    public static OtherPojo rtOtherPojo(Map data, Object obj){

        String jsonStr= JSON.toJSONString(data);
        JSONObject jsondata=JSONObject.parseObject(jsonStr);
        JSONObject jsonObject=jsondata.getJSONObject("data");
        OtherPojo otherPojo = new OtherPojo();
        otherPojo.setToken(jsonObject.getString("token"));
        otherPojo.setDeviceId(jsonObject.getString("deviceId"));

        if(obj!=null){
            String objStr = jsonObject.getString("obj");
            if(!StringUtils.isEmpty(objStr)){
                Object cls=JSONObject.parseObject(objStr, obj.getClass());
                otherPojo.setObj(cls);
            }
        }

        otherPojo.setPage(jsonObject.getIntValue("page"));
        otherPojo.setMsg(jsonObject.getString("msg"));
        return otherPojo;
    }
}
